<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>外卖订餐 - 登录</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            display: flex;
            min-height: 100vh;
            background: linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%);
            overflow: hidden;
        }

        .container {
            display: flex;
            width: 100%;
            max-width: 1200px;
            margin: auto;
            border-radius: 20px;
            overflow: hidden;
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15);
        }

        .left-panel {
            flex: 1;
            background: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url('https://images.unsplash.com/photo-1513104890138-7c749659a591?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80') center/cover no-repeat;
            color: white;
            padding: 50px;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        .app-name {
            font-size: 2.8rem;
            font-weight: 700;
            margin-bottom: 10px;
            color: #ff6b6b;
        }

        .app-tagline {
            font-size: 1.5rem;
            margin-bottom: 30px;
            opacity: 0.9;
        }

        .features {
            list-style: none;
            margin-top: 30px;
        }

        .features li {
            margin-bottom: 15px;
            font-size: 1.1rem;
            display: flex;
            align-items: center;
        }

        .features i {
            color: #ff6b6b;
            margin-right: 10px;
            font-size: 1.2rem;
        }

        .right-panel {
            flex: 1;
            background: white;
            padding: 60px 50px;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        .login-header {
            margin-bottom: 40px;
            text-align: center;
        }

        .login-header h2 {
            font-size: 2.2rem;
            color: #333;
            margin-bottom: 10px;
        }

        .login-header p {
            color: #777;
            font-size: 1.1rem;
        }

        .form-group {
            margin-bottom: 25px;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #555;
        }

        .input-with-icon {
            position: relative;
        }

        .input-with-icon i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #888;
        }

        .form-control {
            width: 100%;
            padding: 15px 15px 15px 45px;
            border: 1px solid #ddd;
            border-radius: 10px;
            font-size: 1rem;
            transition: all 0.3s;
        }

        .form-control:focus {
            border-color: #ff6b6b;
            box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.2);
            outline: none;
        }

        .remember-forgot {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
            font-size: 0.95rem;
        }

        .remember-me {
            display: flex;
            align-items: center;
        }

        .remember-me input {
            margin-right: 8px;
        }

        .forgot-password {
            color: #ff6b6b;
            text-decoration: none;
            transition: color 0.3s;
        }

        .forgot-password:hover {
            color: #ff5252;
            text-decoration: underline;
        }

        .btn-login {
            background: #ff6b6b;
            color: white;
            border: none;
            padding: 15px;
            border-radius: 10px;
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            transition: background 0.3s, transform 0.2s;
            width: 100%;
            margin-bottom: 25px;
        }

        .btn-login:hover {
            background: #ff5252;
            transform: translateY(-2px);
        }

        .btn-login:active {
            transform: translateY(0);
        }

        .social-login {
            text-align: center;
            margin-bottom: 25px;
        }

        .social-login p {
            position: relative;
            color: #888;
            margin-bottom: 20px;
        }

        .social-login p::before,
        .social-login p::after {
            content: "";
            position: absolute;
            top: 50%;
            width: 30%;
            height: 1px;
            background: #ddd;
        }

        .social-login p::before {
            left: 0;
        }

        .social-login p::after {
            right: 0;
        }

        .social-icons {
            display: flex;
            justify-content: center;
            gap: 15px;
        }

        .social-icon {
            width: 45px;
            height: 45px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1.2rem;
            cursor: pointer;
            transition: transform 0.3s;
        }

        .social-icon:hover {
            transform: translateY(-3px);
        }

        .facebook {
            background: #3b5998;
        }

        .google {
            background: #dd4b39;
        }

        .apple {
            background: #000000;
        }

        .signup-link {
            text-align: center;
            font-size: 1rem;
            color: #666;
        }

        .signup-link a {
            color: #ff6b6b;
            text-decoration: none;
            font-weight: 500;
            margin-left: 5px;
        }

        .signup-link a:hover {
            text-decoration: underline;
        }

        .error-message {
            color: #e74c3c;
            background: #fceae9;
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            display: none;
            text-align: center;
        }

        @media (max-width: 900px) {
            .container {
                flex-direction: column;
                max-width: 500px;
            }

            .left-panel {
                display: none;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <div class="left-panel">
        <div class="app-name">美食速递</div>
        <div class="app-tagline">发现美食，即刻送达</div>
        <ul class="features">
            <li><i class="fas fa-shipping-fast"></i> 30分钟内快速送达</li>
            <li><i class="fas fa-utensils"></i> 超过1000家精选餐厅</li>
            <li><i class="fas fa-percent"></i> 新用户首单立减20元</li>
            <li><i class="fas fa-shield-alt"></i> 100%食品安全保障</li>
            <li><i class="fas fa-headset"></i> 24小时客服支持</li>
        </ul>
    </div>

    <div class="right-panel">
        <div class="login-header">
            <h2>欢迎回来</h2>
            <p>登录您的账号，开启美食之旅</p>
        </div>

        <div class="error-message" id="errorMessage" style="display:none"></div>

        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名或手机号</label>
                <div class="input-with-icon">
                    <i class="fas fa-user"></i>
                    <input type="text" id="username" name="username" class="form-control" placeholder="请输入用户名或手机号" required>
                </div>
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <div class="input-with-icon">
                    <i class="fas fa-lock"></i>
                    <input type="password" id="password" name="password" class="form-control" placeholder="请输入密码" required>
                </div>
            </div>

            <div class="remember-forgot">
                <div class="remember-me">
                    <input type="checkbox" id="remember">
                    <label for="remember">记住我</label>
                </div>
                <a href="#" class="forgot-password">忘记密码?</a>
            </div>

            <button type="submit" class="btn-login">登录</button>
        </form>

        <div class="social-login">
            <p>使用其他方式登录</p>
            <div class="social-icons">
                <div class="social-icon facebook">
                    <i class="fab fa-facebook-f"></i>
                </div>
                <div class="social-icon google">
                    <i class="fab fa-google"></i>
                </div>
                <div class="social-icon apple">
                    <i class="fab fa-apple"></i>
                </div>
            </div>
        </div>

        <div class="signup-link">
            还没有账号? <a href="http://localhost:8030/registration.html">立即注册</a>
        </div>
    </div>
</div>

<!-- 登录失败弹窗 -->
<div id="loginModal" style="display:none; position:fixed; left:0; top:0; width:100vw; height:100vh; background:rgba(0,0,0,0.3); z-index:9999; align-items:center; justify-content:center;">
  <div style="background:#fff; padding:30px 40px; border-radius:10px; text-align:center; min-width:300px;">
    <div id="modalMsg" style="color:#e74c3c; margin-bottom:20px;"></div>
    <button onclick="closeModal()" style="padding:8px 30px; background:#ff6b6b; color:#fff; border:none; border-radius:5px; font-size:1rem;">重新登录</button>
  </div>
</div>

<script>
    window.onload = function() {
        const remembered = localStorage.getItem('rememberedUsername');
        if (remembered) {
            document.getElementById('username').value = remembered;
            document.getElementById('remember').checked = true;
        }
    }
    document.getElementById('loginForm').addEventListener('submit', function(e) {
        e.preventDefault();
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        // const errorMessage = document.getElementById('errorMessage');
        // errorMessage.style.display = 'none'; // 提交前先隐藏

        fetch('/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                if (document.getElementById('remember').checked) {
                    localStorage.setItem('rememberedUsername', username);
                } else {
                    localStorage.removeItem('rememberedUsername');
                }
                // 登录成功，跳转到主页
                window.location.href = '/first.html';
            } else {
                showModal(data.message || "用户名或密码错误，请重试");
            }
        })
        .catch(() => {
            showModal("网络错误，请稍后重试");
        });
    });
    document.getElementById('username').addEventListener('input', function() {
        // document.getElementById('errorMessage').style.display = 'none';
    });
    document.getElementById('password').addEventListener('input', function() {
        // document.getElementById('errorMessage').style.display = 'none';
    });

    // 弹窗相关函数
    function showModal(msg) {
        document.getElementById('modalMsg').textContent = msg;
        document.getElementById('loginModal').style.display = 'flex';
    }
    function closeModal() {
        document.getElementById('loginModal').style.display = 'none';
    }
</script>
</body>
</html>